package jspservlet.servlet;

import jspservlet.dao.UserDao;
import jspservlet.dao.impl.UserDaoImpl;
import jspservlet.vo.Warehouse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/addWarehouse")
public class addWarehouseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int flag=-1;
        Warehouse warehouse = new Warehouse();
        warehouse.setHouseID(Integer.parseInt(req.getParameter("WarehouseID")));
        warehouse.setHouseName(req.getParameter("WarehouseName"));
        int managerID=Integer.parseInt(req.getParameter("managerID"));
        UserDao dao=new UserDaoImpl();

        flag= dao.addToWarehouse(warehouse.getHouseID(), warehouse.getHouseName(), managerID);
        if(flag==1){
            System.out.println("ok");
            resp.sendRedirect("./welcome.jsp");
        }else {
            HttpSession session=req.getSession();
            session.setAttribute("managerIDError", "wrong managerID, please double check");
        }

    }
}
